package com.fhc.view.utils

import android.annotation.SuppressLint
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlin.random.Random

@SuppressLint("ConfigurationScreenWidthHeight")
@Composable
fun rememberScreenHeight(): Dp {
    val configuration = LocalConfiguration.current
    return with(LocalDensity.current) {
        configuration.screenHeightDp.dp
    }
}

fun randomColor() = Color(
    Random.nextInt(256),
    Random.nextInt(256),
    Random.nextInt(256)
)

//基础色轮对比色算法‌ 将RGB转换为HSV空间后旋转色相180°：  适合视觉设计用方案 需要纯色对比的场景
fun getComplementaryColor(seed: Color): Color {
    val hsv = FloatArray(3)
    android.graphics.Color.colorToHSV(seed.toArgb(), hsv)
    hsv[0] = (hsv[0] + 180) % 360 // 色相旋转180度
    return Color(android.graphics.Color.HSVToColor(hsv))
}

//增强对比度方案‌ 结合WCAG 2.0标准确保可读性  推荐用于文本/背景组合
fun getAccessibleContrast(seed: Color): Color {
    // 计算相对亮度（公式来自WCAG 2.0）
    val luminance = 0.2126 * seed.red +
            0.7152 * seed.green +
            0.0722 * seed.blue
    return if (luminance > 0.5) Color.Black else Color.White
}
//三色对比系统‌ 生成分裂互补色（Split Complementary）： 生成与原始色形成120°和240°夹角的两个对比色。  适用于平面设计,多色搭配设计
fun getTriadicColors(seed: Color): List<Color> {
    val hsv = FloatArray(3)
    android.graphics.Color.RGBToHSV(
        (seed.red * 255).toInt(),
        (seed.green * 255).toInt(),
        (seed.blue * 255).toInt(),
        hsv
    )
    return listOf(
        Color.hsv((hsv[0] + 120) % 360, hsv[1], hsv[2]),
        Color.hsv((hsv[0] + 240) % 360, hsv[1], hsv[2])
    )
}

fun rgbToHsv(color: Color): FloatArray {
    val r = color.red
    val g = color.green
    val b = color.blue
    val max = maxOf(r, g, b)
    val min = minOf(r, g, b)
    val delta = max - min

    val h = when {
        delta == 0f -> 0f
        max == r -> 60 * ((g - b) / delta % 6)
        max == g -> 60 * ((b - r) / delta + 2)
        else -> 60 * ((r - g) / delta + 4)
    }.let { if (it < 0) it + 360 else it }

    val s = if (max == 0f) 0f else delta / max
    val v = max
    return floatArrayOf(h, s, v)
}

class BottomCornersShape(private val cornerSize: Dp) : Shape {
    override fun createOutline(
        size: Size,
        layoutDirection: LayoutDirection,
        density: Density
    ) =
        RoundedCornerShape(
            bottomStart = cornerSize,
            bottomEnd = cornerSize
        ).createOutline(size, layoutDirection, density)

}